define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/retreat/index' + location.search,
                    // add_url: 'purchase/retreat/add',
                    // edit_url: 'purchase/retreat/edit',
                    // del_url: 'purchase/retreat/del',
                    // multi_url: 'purchase/retreat/multi',
                    table: 'purchase_retreat',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 6) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'retreat_id'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'purchaseenter.number', title: __('Purchaseenter.number')},
                        {field: 'purchaseorder.number', title: __('Purchaseorder.number')},
                        {field: 'collectiontime', title: __('Collectiontime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD",operate:false},
                        {field: 'operator', title: __('Operator'),operate:'LIKE'},
                        {field: 'company', title: __('Company'),operate:'LIKE'},
                        // {field: 'tax', title: __('Tax'), operate:'BETWEEN'},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'supplier.nickname', title: __('Supplier.nickname'),operate:'LIKE'},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=4")},
                        {field: 'explain', title: __('Explain')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('采购单审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var retreat_id = $(this).parents('tr').find('.retreat_id').html()
                                        var url = Config.this_url+'/purchase/retreat/detail?ids='+retreat_id
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "退货详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                        // var retreat_id =$(this).parents('tr').find('.retreat_id').html()
                                        // // var url = Config.this_url+'/purchase/order/enter?ids='+oid
                                        // layer.confirm('是否通过该采购单？', {
                                        //     btn: ['确定','驳回'] //按钮
                                        // }, function(){
                                        //     var url = Config.this_url+'/purchase/retreat/examine?ids='+retreat_id
                                        //     $.post(url,{status:1},function (res) {
                                        //         layer.msg(res.msg)
                                        //         if(res.code == 1){
                                        //             $('.btn-refresh').click()
                                        //         }
                                        //     })
                                        // }, function(){
                                        //     var url = Config.this_url+'/purchase/retreat/examine?ids='+retreat_id
                                        //     $.post(url,{status:5},function (res) {
                                        //         layer.msg(res.msg)
                                        //         if(res.code == 1){
                                        //             $('.btn-refresh').click()
                                        //         }
                                        //     })
                                        // });

                                    }},
                                {
                                    name: 'returnorder',
                                    title: __('出库'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-level-down',
                                    hidden:function(row){
                                        if(row.ystatus != 2){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var retreat_id =$(this).parents('tr').find('.retreat_id').html()
                                        layer.confirm('是否已经出库', {
                                            time: 20000, //20s后自动关闭
                                            btn: ['是的', '还没']
                                        },function () {

                                                var url = Config.this_url+'/purchase/retreat/examine?ids='+retreat_id
                                                $.post(url,{status:3},function (res) {
                                                    layer.msg(res.msg)
                                                    if(res.code == 1){
                                                        $('.btn-refresh').click()
                                                    }
                                                })

                                        },function () {

                                        });
                                    }},
                                {
                                    name: 'returnorder',
                                    title: __('确认收货'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa  fa-check',
                                    hidden:function(row){
                                        if(row.ystatus != 3){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var retreat_id =$(this).parents('tr').find('.retreat_id').html()
                                        layer.confirm('确认完结', {
                                            time: 20000, //20s后自动关闭
                                            btn: ['是的', '还没']
                                        },function () {

                                            var url = Config.this_url+'/purchase/retreat/examine?ids='+retreat_id
                                            $.post(url,{status:4},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })

                                        },function () {

                                        });
                                    }},

                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var retreat_id = $(this).parents('tr').find('.retreat_id').html()
                var url = Config.this_url+'/purchase/retreat/detail?ids='+retreat_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "退货详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/purchase/retreat/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});